import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
// import { VitePWA } from "vite-plugin-pwa";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import AutoImport from "unplugin-auto-import/vite";
import ElementPlus from "unplugin-element-plus/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Inspect from "vite-plugin-inspect";
import viteCompression from "vite-plugin-compression";
import vueI18n from "@intlify/vite-plugin-vue-i18n";
import wasm from "vite-plugin-wasm";
import _ from "lodash";
import { zhCn } from "element-plus/es/locale";
const pathSrc = path.resolve(__dirname, "./src");

const getEnv = (env) => {
  let mode = _.last(process.argv);
  if (mode.indexOf("=") != -1) {
    mode = mode.split("=")[1];
  }
  return loadEnv(mode, process.cwd())[env];
};
const mode = getEnv("VITE_NODE_ENV"); //development
export default defineConfig({
  base: mode === "production" ? "./" : "",

  resolve: {
    alias: {
      "@": pathSrc,
      "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js",
    },
  },
  css: {
    //css预处理
    preprocessorOptions: {
      scss: {
        /*
				引入var.scss全局预定义变量，
				如果引入多个文件，
				可以使用
				'@import "@/assets/scss/globalVariable1.scss";@import "@/assets/scss/globalVariable2.scss";'
				这种格式
				 */
        additionalData: '@use "/src/assets/scss/globalVariable.scss";',
      },
    },
  },
  plugins: [
    AutoImport({
      // Auto import functions from Vue, e.g. ref, reactive, toRef...
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ["vue", "vue-router", "vue-i18n"],

      // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
      // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
      resolvers: [
        ElementPlusResolver(),
        // Auto import icon components
        // 自动导入图标组件
        IconsResolver({
          prefix: "Icon",
        }),
      ],

      dts: path.resolve(pathSrc, "auto-imports.d.ts"),
    }),

    Components({
      resolvers: [
        // Auto register Element Plus components
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
        // Auto register icon components
        // 自动注册图标组件
        IconsResolver(),
      ],

      dts: path.resolve(pathSrc, "components.d.ts"),
    }),
    ElementPlus({
      useSource: true,
    }),
    Icons({
      compiler: "vue3",
      autoInstall: true,
    }),
    Inspect(),
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      // algorithm: "gzip",
      // ext: ".gz",
      ext: ".br",
      algorithm: "brotliCompress",
    }),

    vue(),
    vueI18n({
      include: path.resolve(__dirname, "./path/to/src/locales/**"),
    }),
    wasm(),
    // VitePWA({
    //   // strategies: 'injectManifest',
    //   // srcDir: 'src',
    //   // filename: 'my-sw.js',
    //   // injectRegister: "auto",
    //   // registerType: "autoUpdate",
    //   // workbox: {
    //   //   globPatterns: ["**/*{js,css,html,ico,png,svg,ttf,mp4,gif,flac}"],
    //   // },
    //   includeAssets: [
    //     "favicon.ico",
    //     "apple-touch-icon-180x180.png",
    //     "safari-pinned-tab.svg",
    //   ],
    //   manifest: {
    //     name: "我的博客",
    //     short_name: "我的博客",
    //     description: "我的博客",
    //     theme_color: "#ffffff",
    //     icons: [
    //       {
    //         src: "/img/icons/android-chrome-192x192.png",
    //         sizes: "192x192",
    //         type: "image/png",
    //       },
    //       {
    //         src: "/img/icons/android-chrome-512x512.png",
    //         sizes: "512x512",
    //         type: "image/png",
    //       },
    //       {
    //         src: "/img/icons/android-chrome-512x512.png",
    //         sizes: "512x512",
    //         type: "image/png",
    //         purpose: "any maskable",
    //       },
    //     ],
    //   },
    // }),
  ],
  server: {
    host: "0.0.0.0",
    proxy: {
      "/api": {
        target: "https://ls-gc-zy.com/", //接口域名
        changeOrigin: true, //是否跨域
        ws: true, //是否代理 websockets
        secure: true, //是否https接口
        rewrite:
          //路径重置
          (path) => path.replace(/^\/api/, ""),
      },
    },
  },

  optimizeDeps: {
    esbuildOptions: { target: "esnext" },
  },
  build: {
    //传递给 Terser 的更多 minify 选项。 生产环境去除 console debugger
    target: "esnext",
    minify: "terser",
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("echarts")) {
            return "echarts";
          }
          if (id.includes("data")) {
            return "data";
          }
          if (id.includes("editor")) {
            return "editor";
          }
          if (id.includes("system")) {
            return "system";
          }
          if (id.includes("tools")) {
            return "tools";
          }
          if (id.includes("views")) {
            return "views";
          }
        },
      },
    },
    chunkSizeWarningLimit: 3000,
  },
});
